import flash.events.ContextMenuEvent;

import mx.containers.Canvas;
import mx.controls.TextArea;
import mx.controls.textClasses.TextRange;
import mx.events.FlexEvent;
	

[Bindable]
private var cm:ContextMenu;	


private function createContextMenu(event:FlexEvent):void{
	cm = new ContextMenu();
	addCustomMenuItems();
	cm.clipboardMenu = true;
	  	      			
  	textArea.contextMenu = cm;
  	      			
  	var txt:TextField = textArea.mx_internal::getTextField() as TextField;
  	      			
	txt.contextMenu = cm;

} 

private function menuItemSelectHandler(event:Event):void{
	
	var trgt:NativeMenuItem = (event.target as NativeMenuItem);
	
	var mySelectedTextRange:TextRange = new TextRange(textArea, true);
	
	var canvas:*;
	
	switch(trgt.label){
		case "Add Link...":
			 var w:AddLinkDialog = AddLinkDialog(
       			 PopUpManager.createPopUp(this, AddLinkDialog, true)
   			 );
   	 		w.textEditor = this;
     		PopUpManager.centerPopUp(w);			
		break;
		case "Chapter":			
		break;		
		case "Character":			
		break;
		case "Event":
		break;
		case "Location":
		break;
	}	
}
			
private function addCustomMenuItems():void{		
	var item:ContextMenuItem;				
	
	item = new ContextMenuItem("Add Link...");
	item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
	cm.addItem(item);
	
	item = new ContextMenuItem("Remove Link");
	item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
	cm.addItem(item);
	
	var submenu:NativeMenu = new NativeMenu();
	var subitem:NativeMenuItem;
	
	subitem = new NativeMenuItem("Chapter");
	subitem.addEventListener(Event.SELECT, menuItemSelectHandler);
	submenu.addItem(subitem);
	subitem = new NativeMenuItem("Character");
	subitem.addEventListener(Event.SELECT, menuItemSelectHandler);
	submenu.addItem(subitem);
	subitem = new NativeMenuItem("Event");
	subitem.addEventListener(Event.SELECT, menuItemSelectHandler);
	submenu.addItem(subitem);
	subitem = new NativeMenuItem("Location");
	subitem.addEventListener(Event.SELECT, menuItemSelectHandler);
	submenu.addItem(subitem);
				
	cm.addSubmenu(submenu, "New");            	
}